package com.example.managemyself.data.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.managemyself.data.model.DiaryEntry;

import java.util.Date;
import java.util.List;

@Dao
public interface DiaryEntryDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long insertDiaryEntry(DiaryEntry diaryEntry);
    
    @Update
    void updateDiaryEntry(DiaryEntry diaryEntry);
    
    @Delete
    void deleteDiaryEntry(DiaryEntry diaryEntry);
    
    @Query("DELETE FROM diary_entries WHERE id = :id")
    void deleteDiaryEntryById(int id);
    
    @Query("SELECT * FROM diary_entries WHERE id = :id")
    LiveData<DiaryEntry> getDiaryEntryById(int id);
    
    @Query("SELECT * FROM diary_entries ORDER BY date DESC")
    LiveData<List<DiaryEntry>> getAllDiaryEntries();
    
    @Query("SELECT * FROM diary_entries WHERE date BETWEEN :startDate AND :endDate ORDER BY date DESC")
    LiveData<List<DiaryEntry>> getDiaryEntriesByDateRange(Date startDate, Date endDate);
    
    @Query("SELECT * FROM diary_entries WHERE content LIKE '%' || :keyword || '%' ORDER BY date DESC")
    LiveData<List<DiaryEntry>> searchDiaryEntries(String keyword);
    
    @Query("SELECT * FROM diary_entries WHERE date BETWEEN :startOfDay AND :endOfDay ORDER BY date DESC")
    LiveData<DiaryEntry> getDiaryEntryByDate(long startOfDay, long endOfDay);
    
    @Query("SELECT * FROM diary_entries WHERE date BETWEEN :startOfDay AND :endOfDay ORDER BY date DESC LIMIT 1")
    DiaryEntry getDiaryByDateSync(long startOfDay, long endOfDay);
    
    // 同步查询方法供AIDataManager使用
    @Query("SELECT * FROM diary_entries ORDER BY date DESC LIMIT :days")
    List<DiaryEntry> getRecentDiariesSync(int days);
    
    // AI助手专用的同步查询方法
    @Query("SELECT * FROM diary_entries WHERE date >= :startDate ORDER BY date DESC")
    List<DiaryEntry> getDiariesAfterDateSync(Date startDate);
    
    @Query("SELECT * FROM diary_entries WHERE DATE(date) = DATE(:targetDate) ORDER BY date DESC")
    List<DiaryEntry> getDiariesByDateSync(Date targetDate);
    
    @Query("SELECT * FROM diary_entries WHERE title LIKE :keyword OR content LIKE :keyword ORDER BY date DESC LIMIT 10")
    List<DiaryEntry> searchDiariesSync(String keyword);

    // ==================== 用户数据隔离查询方法 ====================
    
    @Query("SELECT * FROM diary_entries WHERE userId = :userId ORDER BY date DESC")
    LiveData<List<DiaryEntry>> getDiaryEntriesByUserId(int userId);
    
    @Query("SELECT * FROM diary_entries WHERE id = :id AND userId = :userId")
    LiveData<DiaryEntry> getDiaryEntryByIdAndUserId(int id, int userId);
    
    @Query("SELECT * FROM diary_entries WHERE date BETWEEN :startOfDay AND :endOfDay AND userId = :userId ORDER BY date DESC")
    LiveData<DiaryEntry> getDiaryEntryByDateAndUserId(long startOfDay, long endOfDay, int userId);
    
    @Query("SELECT * FROM diary_entries WHERE date BETWEEN :startOfDay AND :endOfDay AND userId = :userId ORDER BY date DESC LIMIT 1")
    DiaryEntry getDiaryByDateAndUserIdSync(long startOfDay, long endOfDay, int userId);
    
    @Query("SELECT * FROM diary_entries WHERE date BETWEEN :startDate AND :endDate AND userId = :userId ORDER BY date DESC")
    LiveData<List<DiaryEntry>> getDiaryEntriesByDateRangeAndUserId(Date startDate, Date endDate, int userId);
    
    @Query("SELECT * FROM diary_entries WHERE (content LIKE '%' || :keyword || '%' OR title LIKE '%' || :keyword || '%') AND userId = :userId ORDER BY date DESC")
    LiveData<List<DiaryEntry>> searchDiaryEntriesByUserId(String keyword, int userId);
    
    @Query("SELECT * FROM diary_entries WHERE id = :id")
    DiaryEntry getDiaryByIdSync(int id);
    
    @Query("SELECT * FROM diary_entries WHERE userId = :userId ORDER BY date DESC LIMIT :days")
    List<DiaryEntry> getRecentDiariesByUserIdSync(int userId, int days);
    
    @Query("SELECT * FROM diary_entries WHERE userId = :userId AND date >= :startDate ORDER BY date DESC")
    List<DiaryEntry> getDiariesAfterDateByUserIdSync(int userId, Date startDate);
    
    @Query("SELECT * FROM diary_entries WHERE userId = :userId AND DATE(date) = DATE(:targetDate) ORDER BY date DESC")
    List<DiaryEntry> getDiariesByDateAndUserIdSync(int userId, Date targetDate);
    
    @Query("SELECT * FROM diary_entries WHERE userId = :userId AND (title LIKE :keyword OR content LIKE :keyword) ORDER BY date DESC LIMIT 10")
    List<DiaryEntry> searchDiariesByUserIdSync(int userId, String keyword);

}
